<?php
declare(strict_types=1);


namespace App\Game\Mapper;

use App\Fastapp\Model\FastappChannelAgent;
use App\Game\Model\GameAccount;
use App\Game\Model\GameBet;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 下注表Mapper类
 */
class GameBetMapper extends AbstractMapper
{
    /**
     * @var GameBet
     */
    public $model;

    public function assignModel()
    {
        $this->model = GameBet::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        // 用户id
        if (isset($params['user_id']) && filled($params['user_id'])) {
            $query->where('user_id', '=', $params['user_id']);
        }
        $admin_id = user()->getId();
        $uid = FastappChannelAgent::query()->where(['admin_id' => $admin_id])->value('user_id') ?: 0;
        if ($uid) {
            $uids = GameAccount::query()->where('link', 'like', $uid . '|%')->pluck('user_id');
            $query->whereIn('user_id', $uids);
        }
        return $query->orderByDesc('id');
    }
}